package pl.prv.polanie.client.widgets.table;

import pl.prv.polanie.client.Planner;
import pl.prv.polanie.client.dialogs.CreateCalendarDayDialog;
import pl.prv.polanie.client.dto.CSCalendarDayDTO;
import pl.prv.polanie.client.dto.CSVCalendarDayDTO;
import pl.prv.polanie.client.dto.ICSVDTO;
import pl.prv.polanie.client.model.Cache;
import pl.prv.polanie.client.model.CacheService;
import pl.prv.polanie.client.model.ServiceHolder;
import pl.prv.polanie.client.model.ViewDataProvider;
import pl.prv.polanie.client.panels.lists.CalendarDaysList;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Janusz Prokulewicz
 * Created: Jun 10, 2008
 */
public class TableCalendarDays extends Composite {
	
	private CalendarDaysList parentCalendarDaysList = null;

	
	
	private CustomTable mainCT = new CustomTable(){
		
		@Override
		protected void runOnClickSelectedRow(int row, int column,
				ICSVDTO csvDto, Widget w) {
			super.runOnClickSelectedRow(row, column, csvDto, w);
			
			
			Planner.log("TableLecturers.runOnClickSelectedRow: " + "" + csvDto + "  ------- row " + row + ", col " + column);
			
			CSCalendarDayDTO csCalendarDayDTO = (CSCalendarDayDTO)mainCT.getCSDtoByID(csvDto.getId());
			
			//wyswietlenie okienka do edycji
			CreateCalendarDayDialog ccdd = new CreateCalendarDayDialog(parentCalendarDaysList, csCalendarDayDTO); 
			ccdd.center();
			ccdd.setFocus();
		}
		
		@Override
		protected void runOnSelectRow(ICSVDTO csvDto) {
			super.runOnSelectRow(csvDto);
			
			Planner.log("TableLecturers.runOnSelectRow: " + "wybrano wiersz id " + csvDto.getId() );

			if (parentCalendarDaysList != null) {
				parentCalendarDaysList.showRemoveSelectedRecordButton();
			}
		}

		
	};
	
	public TableCalendarDays(CalendarDaysList calendarDaysList) {
		
		parentCalendarDaysList = calendarDaysList;
		
		if (CacheService.isFullTimeSemesterSelected()) {
			mainCT.setColumnCount(2);
		} else {
			mainCT.setColumnCount(3);
		}
		initWidget(mainCT);
		
		mainCT.addHeaderColumn("day label", 80);
		mainCT.addHeaderColumn("display order", 120);
		if (!CacheService.isFullTimeSemesterSelected()) {
			mainCT.addHeaderColumn("saturday/sunday", 150);
		}
		
//		mainCT.addHeaderColumn(" ", 20);
		
//		loadData();

	}
	
	public void loadData() {
		
		
//		for (int i = 0; i < 40; i++) {
//			long no = i;
//			long no2 = no % 10; 
//			CSSubjectEventDTO cs = new CSSubjectEventDTO(no, 1, (long)2, (long)2, (long)1, (long)1, (long)1, new Integer(660), new Integer(100));
//			CSVSubjectEventDTO csv = new CSVSubjectEventDTO(no, 2, "subject " + (no + no2), "room " + no, "subgroup " + no, "lect " + no,
//					"poniedzialek", new Integer(660), new Integer(100));
//			
//			mainCT.addRow(cs, csv);
//		}
		
		
		
//		mainCT.setColumnWidth(0, 10);
//		mainCT.setColumnWidth(1, 50);
//		mainCT.setColumnWidth(2, 20);
	}
	
	
	/**
	 * Dodaje do tabeli
	 * @param cs
	 * @param csv
	 */
	public void addRow(CSCalendarDayDTO cs, CSVCalendarDayDTO csv) {
		mainCT.addRow(cs, csv);
	}
	
	
	public void saveOrUpdateCalendarDayInTable(CSCalendarDayDTO csDto) {
		CSVCalendarDayDTO csvCD = ViewDataProvider.getCSVCalendarDay(csDto);
		mainCT.addOrUpdateDTOInTable(csDto, csvCD);
	}
	
	
	
	/**
	 * Wysyla calla do serwera usuwajacego na bazie lecturera i jesli operacja sie uda to usuwa takze z tabelki.
	 * @param csDto
	 */
	public void removeSelectedCalendarDayFromTable() {

		final CSVCalendarDayDTO csvCD = (CSVCalendarDayDTO)mainCT.getSelectedCSVDto();
		if (csvCD != null) {
			final CSCalendarDayDTO csCD = (CSCalendarDayDTO)mainCT.getCSDtoByID(csvCD.getId());
			
			
			AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
				public void onFailure(Throwable caught) {
					Planner.log("TableCalendarDays.removeSelectedCalendarDayFromTable() FAILURE");
				}
				public void onSuccess(Boolean res) {
					Planner.log("TableCalendarDays.removeSelectedCalendarDayFromTable() SUCCESS -> zwrocone wynik = " + res);

					//TODO dodac do kontrolki tabelki
					if (res.booleanValue() == true) {
						if (mainCT != null) {
							mainCT.removeRow(csvCD);
						}
					} else {
						
					}

					//update w cache
					Cache.removeCSCalendarDay(csCD);
					
				}
			};
			
			ServiceHolder.get().removeCSCalendarDay(csCD, callback);
		}
		
	}	
	
	
	
	
	
	protected void runOnHighlightingRow(ICSVDTO csvDto) {}

	public void highlightRowByID(long id) {
		mainCT.highlightRowByID(id);
	}
	
	public void setSelectedRowByID(long id) {
		mainCT.setSelectedRowByID(id);
	}
	
	public void removeSelectionFromRow() {
		mainCT.removeSelectionFromSelectedRow();
	}
	
	public void removeHighlightingFromRows() {
		mainCT.removeHighlightingFromRows();
	}
	
	public void sortInitialOrder() {
		boolean asc = true;
		mainCT.sortByColumn(1, asc, -1);
	}
}
